﻿using MyECommerceProject.Interfaces;
using MyECommerceProject.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MyECommerceProject.Services
{
    public class InventoryService : IInventoryService
    {
        private readonly IInventoryRepository _inventoryRepository;
        private readonly IProductRepository _productRepository;

        public InventoryService(IInventoryRepository inventoryRepository, IProductRepository productRepository)
        {
            _inventoryRepository = inventoryRepository;
            _productRepository = productRepository;
        }

        //增加一条库存
        public async Task AddInventoryRecordAsync(int productId, bool sell, int count)
        {
            var product = await _productRepository.GetProductByIdAsync(productId);
            if (product == null)
                throw new Exception("Product not found");

            if (sell && product.StockQuantity < count)
                throw new Exception("Insufficient stock");

            var inventory = new Inventory
            {
                ProductId = productId,
                Sell = sell,
                Count = count,
                OperationDate = DateOnly.FromDateTime(DateTime.Now)
            };

            await _inventoryRepository.AddInventoryRecordAsync(inventory);

            if (sell)
            {
                product.SoldGoods += count;
                product.StockQuantity -= count;
            }
            else
            {
                product.StockQuantity += count;
            }

            await _productRepository.UpdateProductAsync(product);
        }


        //获取周期内库存数
        public async Task<IEnumerable<Inventory>> GetInventoryRecordsAsync(int productId, DateOnly startDate, DateOnly endDate)
        {
            return await _inventoryRepository.GetInventoryRecordsAsync(productId, startDate, endDate);
            
        }

        public async Task<IEnumerable<Inventory>> GetInventoriesAsync()
        {
            return await _inventoryRepository.GetInventoriesAsync();
        }

        public async Task<Inventory> GetInventoryByIdAsync(int inventoryId)
        {
            return await _inventoryRepository.GetInventoryByIdAsync(inventoryId);
        }

        public async Task AddInventoryAsync(Inventory inventory)
        {
            await _inventoryRepository.AddInventoryAsync(inventory);
        }

        public async Task UpdateInventoryAsync(Inventory inventory)
        {
            await _inventoryRepository.UpdateInventoryAsync(inventory);
        }

        public async Task DeleteInventoryAsync(int inventoryId)
        {
            await _inventoryRepository.DeleteInventoryAsync(inventoryId);
        }

        //public async Task SellProductAsync(int productId, int quantity)
        //{
        //    var product = await _productRepository.GetProductByIdAsync(productId);
        //    var inventory = await _inventoryRepository.GetInventoryByIdAsync(productId);

        //    if (inventory.StockQuantity < quantity)
        //    {
        //        throw new Exception("Insufficient stock.");
        //    }

        //    product.Count += quantity;
        //    product.ModifiedDate = DateTime.Now;

        //    inventory.StockQuantity -= quantity;
        //    inventory.LastUpdated = DateTime.Now;

        //    if (inventory.StockQuantity < inventory.StockWarningLevel)
        //    {
        //        // Trigger warning (e.g., logging, notifying admin, etc.)
        //    }

        //    await _productRepository.UpdateProductAsync(product);
        //    await _inventoryRepository.UpdateInventoryAsync(inventory);
        //}

        //public async Task RestockProductAsync(int productId, int quantity)
        //{
        //    var inventory = await _inventoryRepository.GetInventoryByIdAsync(productId);
        //    inventory.StockQuantity += quantity;
        //    inventory.LastUpdated = DateTime.Now;

        //    await _inventoryRepository.UpdateInventoryAsync(inventory);
        //}
    }
}
